package au.com.somethingwritten.controller;

import au.com.somethingwritten.model.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class WebController extends HttpServlet {

	public void doPost( HttpServletRequest request,
						HttpServletResponse response)
						throws IOException, ServletException {
		//Create a book manager
	    BookManager bookMan = new BookManager();

	    //get list of all books from book manager
	    ArrayList<Book> allBooks = null;
	    try{
	    	allBooks = bookMan.getAllBooks();
	    }catch(Exception e){
	    	
	    }

	    //store book data in the request
	    request.setAttribute("books", allBooks);
	    //forward the data to the view
	    RequestDispatcher view = request.getRequestDispatcher("index.jsp");
	    view.forward(request, response); 
	}
	
	public void doGet( HttpServletRequest request,
			HttpServletResponse response)
			throws IOException, ServletException {
			
			//create a new book manager
			BookManager bookMan = new BookManager();
			//get all books in database
			ArrayList<Book> allBooks = bookMan.getAllBooks();
			//store books in the request
			request.setAttribute("books", allBooks);
			//forward data to the view
			RequestDispatcher view = request.getRequestDispatcher("homePage.jsp");
			view.forward(request, response); 
	}
}
